package controllers

import (
	"path"
	"time"
	"github.com/astaxie/beego"
)

type BaseController struct {
	beego.Controller
}

func init() {
	beego.AddFuncMap("prepage",ShowPrePage)
	beego.AddFuncMap("nextpage",ShowNextPage)
}

func (c *BaseController) UploadFile(uploadname string) string  {
	file, head, err := c.GetFile(uploadname)
	if err != nil {
		beego.Info("文件上传发生错误")
		return "no_img"
	}
	defer file.Close()

	if head.Size > 50000000 {
		c.Data["errmsg"] = "文件过大，上传失败"
		return "no_img"
	}

	c.Data["test"]="这是一个测试"

	ext := path.Ext(head.Filename)
	if ext != ".jpg" && ext != ".png" && ext != ".jpeg" {
		c.Data["errmsg"] = "文件格式错误。请重新上传"
		return "no_img"
	}

	fileName := "static/img/"+time.Now().Format("2006-01-02_15:04:05") + ext
	c.SaveToFile("uploadname", fileName)
	return fileName
}

func ShowPrePage(page int) int {
	if page == 1 {
		return page
	}
	return page - 1
}

func ShowNextPage(page int, pageCount int) int {
	if page == pageCount {
		return page
	}
	return page + 1
}
